home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / shared / freeman / probnets.m < prev    next >
Text File  |  1993-10-27  |  4KB  |  134 lines

  1. BeginPackage["ProbabilisticNets`"]
  2.  
  3. normalize::usage = "normalize[x_List]"
  4. energyHop::usage = "energyHop[x,w]"
  5. psi::usage = "psi[inValue,netIn]"
  6. phi::usage = "phi[inVector_List,netInVector_List]"
  7. makeHopfieldWts::usage = "makeHopfieldWts[trainingPats,printWts:True]"
  8. discreteHopfield::usage = "discreteHopfield[wtVector,inVector,printAll:True]"
  9. prob::usage = "prob[n,T]"
  10. probPsi::usage = "probPsi[inValue_,netIn_,temp_]"
  11. stochasticHopfield::usage = "stochasticHopfield[inVector,weights,numSweeps,temp]"
  12. pnnTwoClass::usage = "pnnTwoClass[class1Exemplars,class2Exemplars,testInputs,sig]"
  13.  
  14.  
  15. Begin["`Private`"]    (* begin the private context *)
  16.  
  17. normalize[x_List] := x/(Sqrt[x.x]//N)
  18.  
  19.  
  20. energyHop[x_,w_] := -0.5 x . w . x;
  21.  
  22.  
  23. psi[inValue_,netIn_] := If[netIn>0,1, 
  24.                          If[netIn<0,-1,inValue]] 
  25.  
  26.   
  27. phi[inVector_List,netInVector_List] :=
  28.     MapThread[psi[#,#2]&,{inVector,netInVector}]
  29.  
  30.  
  31. makeHopfieldWts[trainingPats_,printWts_:True] :=
  32.     Module[{wtVector},
  33.      wtVector = 
  34.        Apply[Plus,Map[Outer[Times,#,#]&,trainingPats]];
  35.      If[printWts,
  36.          Print[];
  37.          Print[MatrixForm[wtVector]];
  38.          Print[];,Continue
  39.          ];  (* end of If  *)
  40.      Return[wtVector];
  41.      ] (* end of Module *)
  42.  
  43.  
  44. discreteHopfield[wtVector_,inVector_,printAll_:True] :=
  45.     Module[{done, energy, newEnergy, netInput, 
  46.                 newInput, output},
  47.      done = False;
  48.      newInput = inVector;
  49.      energy = energyHop[inVector,wtVector];
  50.      If[printAll,
  51.          Print[];Print["Input vector = ",inVector];
  52.          Print[];
  53.          Print["Energy = ",energy];
  54.          Print[],Continue
  55.          ];   (* end of If  *)
  56.      While[!done,
  57.          netInput = wtVector . newInput;
  58.          output = phi[newInput,netInput];
  59.          newEnergy = energyHop[output,wtVector];
  60.         If[printAll,
  61.               Print[];Print["Output vector = ",output];
  62.               Print[];
  63.               Print["Energy = ",newEnergy];
  64.               Print[],Continue
  65.               ];   (* end of If  *)
  66.          If[energy==newEnergy,
  67.              done=True,
  68.              energy=newEnergy;newInput=output,
  69.              Continue
  70.              ];   (* end of If  *)
  71.            ];  (* end of While  *)
  72.          If[!printAll, 
  73.           Print[];Print["Output vector = ",output];
  74.           Print[];
  75.           Print["Energy = ",newEnergy];
  76.           Print[];
  77.            ];  (* end of If *)
  78.           ];  (* end of Module  *)      
  79.  
  80.  
  81. prob[n_,T_] := 1/(1+E^(-n/T)) //N;
  82.  
  83.  
  84. probPsi[inValue_,netIn_,temp_] := 
  85.  If[Random[]<=prob[netIn,temp],1,psi[inValue,netIn]];
  86.  
  87. stochasticHopfield[inVector_,weights_,numSweeps_,temp_]:= 
  88.   Module[ {input, net, indx, numUnits, indxList, output},
  89.     numUnits=Length[inVector];
  90.     indxList=Table[0,{numUnits}];
  91.     input=inVector;
  92.     For[i=1,i<=numSweeps,i++,
  93.     Print["i= ",i];
  94.       For[j=1,j<=numUnits,j++,
  95.               (* select unit *)
  96.         indx = Random[Integer,{1,numUnits}]; 
  97.             (* net input to unit *)
  98.         net=input . weights[[indx]]; 
  99.             (* update input vector *)
  100.         output=probPsi[input[[indx]],net,temp];
  101.         input[[indx]]=output;
  102.         indxList[[indx]]+=1;
  103.       ];  (* end For numUnits *)
  104.     Print[ ];Print["New input vector = "];Print[input];
  105.     ];   (* end For numSweeps *)
  106.   Print[ ];Print["Number of times each unit was updated:"];
  107.   Print[ ];Print[indxList];
  108.   ];   (* end of Module *)
  109.  
  110.  
  111. pnnTwoClass[class1Exemplars_,class2Exemplars_,
  112.                             testInputs_,sig_] :=
  113.     Module[{weightsA,weightsB,inputsNorm,patternAout,
  114.                 patternBout,sumAout,sumBout},
  115.         weightsA = Map[normalize,class1Exemplars];
  116.         weightsB = Map[normalize,class2Exemplars];
  117.         inputsNorm = Map[normalize,testInputs];
  118.         sigma = sig;
  119.         patternAout = 
  120.             gaussOut[inputsNorm . Transpose[weightsA]];
  121.         patternBout = 
  122.             gaussOut[inputsNorm . Transpose[weightsB]];
  123.         sumAout = Map[Apply[Plus,#]&,patternAout];
  124.         sumBout = Map[Apply[Plus,#]&,patternBout];
  125.         outputs = Sign[sumAout-sumBout];
  126.         sigma=.;
  127.         Return[outputs];
  128.         ]    
  129.  
  130.  
  131.  
  132. End[]         (* end the private context *)
  133.  
  134. EndPackage[]  (* end the package context *)